<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
          <el-form-item label="订单编号" prop="orderNo">
          <el-input v-model="dataForm.orderNo" placeholder="订单编号"></el-input>
      </el-form-item>
          <el-form-item label="航段编号 必须" prop="segmentNo">
          <el-input v-model="dataForm.segmentNo" placeholder="航段编号 必须"></el-input>
      </el-form-item>
          <el-form-item label="航程标识，1-去程，2-回程 必须" prop="segmentType">
          <el-input v-model="dataForm.segmentType" placeholder="航程标识，1-去程，2-回程 必须"></el-input>
      </el-form-item>
          <el-form-item label="航班号 必须" prop="flightNo">
          <el-input v-model="dataForm.flightNo" placeholder="航班号 必须"></el-input>
      </el-form-item>
          <el-form-item label="出发日期 必须" prop="beginDate">
          <el-input v-model="dataForm.beginDate" placeholder="出发日期 必须"></el-input>
      </el-form-item>
          <el-form-item label="到达日期 必须" prop="arriveDate">
          <el-input v-model="dataForm.arriveDate" placeholder="到达日期 必须"></el-input>
      </el-form-item>
          <el-form-item label="出发时间 必须" prop="beginTime">
          <el-input v-model="dataForm.beginTime" placeholder="出发时间 必须"></el-input>
      </el-form-item>
          <el-form-item label="到达时间 必须" prop="arriveTime">
          <el-input v-model="dataForm.arriveTime" placeholder="到达时间 必须"></el-input>
      </el-form-item>
          <el-form-item label="出发机场三字码" prop="sAirportCode">
          <el-input v-model="dataForm.sAirportCode" placeholder="出发机场三字码"></el-input>
      </el-form-item>
          <el-form-item label="到达机场三字码" prop="eAirportCode">
          <el-input v-model="dataForm.eAirportCode" placeholder="到达机场三字码"></el-input>
      </el-form-item>
          <el-form-item label="舱位代码" prop="cabinCode">
          <el-input v-model="dataForm.cabinCode" placeholder="舱位代码"></el-input>
      </el-form-item>
          <el-form-item label="折扣" prop="discount">
          <el-input v-model="dataForm.discount" placeholder="折扣"></el-input>
      </el-form-item>
          <el-form-item label="产品类型" prop="productType">
          <el-input v-model="dataForm.productType" placeholder="产品类型"></el-input>
      </el-form-item>
              <el-form-item label="更新人" prop="updator">
          <el-input v-model="dataForm.updator" placeholder="更新人"></el-input>
      </el-form-item>
          <el-form-item label="更新时间" prop="updateDate">
          <el-input v-model="dataForm.updateDate" placeholder="更新时间"></el-input>
      </el-form-item>
      </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: '',
        orderNo: '',
        segmentNo: '',
        segmentType: '',
        flightNo: '',
        beginDate: '',
        arriveDate: '',
        beginTime: '',
        arriveTime: '',
        sAirportCode: '',
        eAirportCode: '',
        cabinCode: '',
        discount: '',
        productType: '',
        creator: '',
        createDate: '',
        updator: '',
        updateDate: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        orderNo: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        segmentNo: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        segmentType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        flightNo: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        beginDate: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        arriveDate: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        beginTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        arriveTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        sAirportCode: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        eAirportCode: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        cabinCode: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        discount: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        productType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        updator: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        updateDate: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/flight/ordersegment/${this.dataForm.id}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/flight/ordersegment/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
